﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BeniBastanYaratWinFormProject
{
    public partial class SatisAylikRapor : Form
    {

        dbbaglantiDataContext db = new dbbaglantiDataContext();
        public SatisAylikRapor()
        {
            InitializeComponent();
        }

        private void SatisAylikRapor_Load(object sender, EventArgs e)
        {

            DateTime now = DateTime.Now;


            cmbAy.DataSource = new BindingSource(System.Globalization.DateTimeFormatInfo.CurrentInfo.MonthNames.Where(m => m != String.Empty).Select((m, i) => new { Name = m, Index = i }).ToDictionary(x => x.Index, x => x.Name), null);
            cmbAy.DisplayMember = "Value";
            cmbAy.ValueMember = "Key";

            cmbAy.SelectedIndex = now.Month - 1;



            for (int i = 2000; i <= now.Year; i++)
            {
                cmbYil.Items.Add(i);
            }

            cmbYil.SelectedIndex = cmbYil.Items.Count - 1;


            TabloyuDoldur();
        }


        private void TabloyuDoldur()
        {
            DateTime baslangic = new DateTime(int.Parse(cmbYil.SelectedItem.ToString()), cmbAy.SelectedIndex + 1, 1, 0, 0, 0);

            DateTime bitis = baslangic.AddMonths(1);

            var sorgu = from x in db.Satislars
                        where (x.satıstarih >= baslangic && x.satıstarih < bitis)
                        join y in db.Urunlers on x.urunid equals y.UrunID into adim1
                        from k in adim1
                        select new
                        {
                            k.UrunAdi,
                            x.adet,
                            x.satıstarih,
                            x.toplamfiyat


                        };

            gridRapor.DataSource = sorgu;

            decimal? toplam = 0;
            foreach (var item in sorgu)
            {
                toplam += item.toplamfiyat;
            }
            lblToplamSatis.Text = sorgu.Count().ToString();
            lblCiro.Text = toplam.ToString();
        }

        private void btnFiltrele_Click(object sender, EventArgs e)
        {
            TabloyuDoldur();
        }
    }
}
